﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors;
using Sunboxsoft.Common;

namespace Sunboxsoft.ASMS.Client.UI
{
    static class GridEmbeddedNavigatorUtil
    {
        /// <summary>
        /// 绑定数据，并显示分页信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="gridControl"></param>
        /// <param name="result"></param>
        /// <param name="currentPageIndex"></param>
        /// <param name="totalPageCount"></param>
        public static void Paginate<T>(this GridControl gridControl, PagedResult<T> result, out int currentPageIndex, out int totalPageCount)
        {
            gridControl.DataSource = result.Data.Items;
            gridControl.UseEmbeddedNavigator = true;

            Update<T>(gridControl.EmbeddedNavigator, result, out currentPageIndex, out totalPageCount);
        }

        /// <summary>
        /// 更新分页栏
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="navigator"></param>
        /// <param name="result"></param>
        /// <param name="currentPageIndex"></param>
        /// <param name="totalPageCount"></param>
        [Obsolete("use Paginate")]
        public static void Update<T>(ControlNavigator navigator, PagedResult<T> result, out int currentPageIndex, out int totalPageCount)
        {
            totalPageCount = result.Data.Total / result.Data.Limit;
            if (result.Data.Total % result.Data.Limit != 0)
            {
                totalPageCount++;
            }

            currentPageIndex = (result.Data.Start + result.Data.Count) / result.Data.Limit;
            if ((result.Data.Start + result.Data.Count) % result.Data.Limit == 0 && currentPageIndex > 0)
            {
                currentPageIndex--;
            }

            if (navigator.Buttons.CustomButtons.Count == 0)
            {
                for(int index = 0; index <navigator.Buttons.ButtonCollection.Count; index++)
                {
                    navigator.Buttons.ButtonCollection[index].Visible = false;
                }

                NavigatorCustomButton btnFirst = new NavigatorCustomButton(-1, 0, "first");
                btnFirst.Tag = NavigatorButtonType.First;
                NavigatorCustomButton btnPrev = new NavigatorCustomButton(-1, 1, "prev");
                btnPrev.Tag = NavigatorButtonType.PrevPage;
                NavigatorCustomButton btnNext = new NavigatorCustomButton(-1, 4, "next");
                btnNext.Tag = NavigatorButtonType.NextPage;
                NavigatorCustomButton btnLast = new NavigatorCustomButton(-1, 5, "last");
                btnLast.Tag = NavigatorButtonType.Last;
                navigator.Buttons.CustomButtons.AddRange(new NavigatorCustomButton[] { btnFirst, btnPrev, btnNext, btnLast });
            }
            navigator.Buttons.PageRecordCount = result.Data.Total;
            navigator.TextStringFormat = string.Format("第{0}页, 共{1}页", currentPageIndex + 1, totalPageCount);
            if (result.Data.Start + result.Data.Count == result.Data.Total)
            {
                // last page

                navigator.CustomButtons[3].Enabled = false;
                navigator.CustomButtons[2].Enabled = false;
            }
            else
            {
                navigator.CustomButtons[3].Enabled = true;
                navigator.CustomButtons[2].Enabled = true;
            }
            
            if (result.Data.Start == 0)
            {
                // first page
                navigator.CustomButtons[0].Enabled = false;
                navigator.CustomButtons[1].Enabled = false;
            }
            else
            {
                navigator.CustomButtons[0].Enabled = true;
                navigator.CustomButtons[1].Enabled = true;
            }
        }

        /// <summary>
        /// 根据分页栏上点击事件来修改当前页码
        /// </summary>
        /// <param name="args"></param>
        /// <param name="pageCount"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static bool UpdatePageIndex(this DevExpress.XtraEditors.NavigatorButtonClickEventArgs args, int pageCount, ref int pageIndex)
        {
            NavigatorCustomButton button = args.Button as NavigatorCustomButton;
            if (button == null)
            {
                return false;
            }
            if ((NavigatorButtonType)button.Tag == NavigatorButtonType.First)
            {
                pageIndex = 0;
                return true;
            }
            else if ((NavigatorButtonType)button.Tag == NavigatorButtonType.PrevPage)
            {
                pageIndex--;
                return true;
            }
            else if ((NavigatorButtonType)button.Tag == NavigatorButtonType.NextPage)
            {
                pageIndex++;
                return true;
            }
            else if ((NavigatorButtonType)button.Tag == NavigatorButtonType.Last)
            {
                pageIndex = pageCount - 1;
                return true;
            }
            return false;
        }
    }
}
